﻿using System;
using System.Linq;

namespace Common
{
    public static class BytesHelper
    {
        /// <summary>
        /// 序列化为16进制字符串
        /// </summary>
        /// <param name="splitChar">分割字符串</param>
        /// <param name="isEsc">是否转义</param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static string ToHexString(this ReadOnlyMemory<byte> bytes, char splitChar = ',')
        {
            try
            {
                return string.Join(splitChar, bytes.ToArray().Select(b => b.ToString("X2")));
            }
            catch (Exception ex)
            {
                throw new Exception("序列化异常:" + ex.Message);
            }
        }
    }
}